package com.project.service;

import com.alibaba.druid.util.StringUtils;
import com.project.common.utils.PublicUtils;
import com.project.dao.RoleResourceMapper;
import com.project.entity.RoleResource;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/25 19:42
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class RoleResourceServiceImpl  extends BaseService implements  RoleResourceService{
    @Autowired
    private static RoleResourceMapper roleResourceMapper;

    @Override
    public int addBatch(RoleResource roleResource){

        //int roleid,String resourceIds
        int roleid=roleResource.getRoleid();

        delByRoleId(roleid); //先全部删除角色当前所拥有的资源，然后再插入

        String resourceIds=roleResource.getResourceids();
        if(!StringUtils.isEmpty(resourceIds)) {
            List<Integer> ids = PublicUtils.getIntIdList(resourceIds, ",");
            for(int id:ids){
                RoleResource roleResource1=new RoleResource();
                roleResource1.setRoleid(roleid);
                roleResource1.setResourceid(id);
                add(roleResource1);
            }
        }

        return 1;
    }

    @Override
    public int add(RoleResource roleResource){

        return roleResourceMapper.insertSelective(roleResource);
    }

    @Override
    public int del(int id){

        return roleResourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<RoleResource> findByRoleId(int roleid){


        return roleResourceMapper.selectByRoleId(roleid);
    }

    @Override
    public int delByRoleId(int roleid){


        return roleResourceMapper.deleteByRoleId(roleid);
    }

    @Override
    public int deleteByResourceId(int resourceid){


        return roleResourceMapper.deleteByResourceId(resourceid);
    }

}
